//
//  LLGoodsBasic.m
//  lrlzapp
//
//  Created by dl on 16/3/2.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLGoodsBasic.h"

@implementation LLGoodsBasic


+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
            @"UUID" : @"uuid",
            @"itemId" : @"item_id",
            @"buyMode" : @"buy_mode",
            @"status" : @"status",
            @"title" : @"title",
            @"brandUUID" : @"brand_uuid",
            @"brandName" : @"brand_name",
            @"ogPrice" : @"og_price",
            @"curPrice" : @"cur_price",
            @"stockCount" : @"stock_count",
            @"sales" : @"sales",
            @"gold" : @"point",
            @"feedbackGold" : @"give_point",
            @"moneyAndGold" : @"pointAndMoney",
            @"imageUrl" : @"pic_url",
            @"tags" : @"labels"
    };
}

+ (NSValueTransformer *)buyModeJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)statusJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)ogPriceJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)curPriceJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)stockCountJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)salesJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)goldJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)feedbackGoldJSONTransformer {
    return [NSValueTransformer valueTransformerForName:MTLCommonValueTransformerName];
}

+ (NSValueTransformer *)tagsJSONTransformer {
    return [MTLValueTransformer transformerUsingForwardBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
        TransformerForwardNull
        if ([value isEqualToString:@""]) {
            return nil;
        }
        return [[value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
                componentsSeparatedByString:@" "];
    }                                           reverseBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
        TransformerReverseNull
        if (![value isKindOfClass:[NSArray class]]) {
            return @"null";
        }
        return [value componentsJoinedByString:@" "];
    }];
}

@end


@implementation LLGoodsBasicBag

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return [super.JSONKeyPathsByPropertyKey mtl_dictionaryByAddingEntriesFromDictionary:
            @{@"goodsBasicItems" : @"list"}];
}

+ (NSValueTransformer *)goodsBasicItemsJSONTransformer {
    return [MTLValueTransformer transformerUsingForwardBlock:^id(NSArray *jsonArray, BOOL *success, NSError *__autoreleasing *error) {
        NSError *localError = nil;
        NSArray *goodsBasicItems = [MTLJSONAdapter modelsOfClass:LLGoodsBasic.class fromJSONArray:jsonArray error:&localError];
        if (localError) {
            JSONArrayToModelArrayError(LLGoodsBasic.class, error, jsonArray);
            return nil;
        }
        return goodsBasicItems;
    }                                           reverseBlock:^id(NSArray *models, BOOL *success, NSError *__autoreleasing *error) {
        NSError *localError = nil;
        NSArray *jsonArray = [MTLJSONAdapter JSONArrayFromModels:models error:&localError];
        if (localError) {
            ModelArrayToJsonArrayError(LLGoodsBasic.class, error, models);
            return nil;
        }

        return jsonArray;
    }];
}

@end